package com.elf.zero.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewSwitcher;

import com.elf.zero.app.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class VerticalViewSwitcher<T> extends ViewSwitcher {

    private int duration = 300;
    private int interval = 1000;

    private Timer timer;
    private TimerTask timerTask;
    private Handler handler = new Handler(Looper.getMainLooper());

    private List<T> items = new ArrayList<>();
    private int index;
    private boolean pause = false;

    private final Object lock = new Object();

    private CreateViewListener<T> createViewListener;


    public VerticalViewSwitcher(Context context) {
        this(context, null);
    }

    public VerticalViewSwitcher(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.VerticalViewSwitcher);
            duration = typedArray.getInt(R.styleable.VerticalViewSwitcher_duration, duration);
            interval = typedArray.getInt(R.styleable.VerticalViewSwitcher_interval, interval);
            typedArray.recycle();
        }

        TranslateAnimation inAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0);
        inAnimation.setDuration(duration);
        TranslateAnimation outAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1);
        outAnimation.setDuration(duration);
        setInAnimation(inAnimation);
        setOutAnimation(outAnimation);
    }

    public void setDataList(List<T> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        stop();
        items = list;

        //显示第一条
        index = 0;
        if (createViewListener != null) {
            try {
                createViewListener.setViewData(getCurrentView(), items.get(index++));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //大于一条开启定时器
        if (items.size() > 1) {
            start();
        }
    }

    public void updateDataList(List<T> list) {
        if (list == null || list.size() == 0) {
            return;
        }

        synchronized (lock) {
            items = list;
            index = 0;

            //暂停状态不处理
            if (pause) {
                return;
            }

            if (timerTask == null) {
                start();
            }
        }
    }

    public void start() {
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (lock) {
                            if (index >= items.size()) {
                                index = 0;
                            }
                            if (index < items.size()) {
                                showNext();
                            }
                            if (items.size() == 1) {
                                stop();
                            }
                        }
                    }
                });
            }
        };
        timer.schedule(timerTask, interval, interval);
    }


    /**
     * 恢复
     */
    public void resume() {
        synchronized (lock) {
            pause = false;
            if (items.size() <= 1) {
                return;
            }
            if (timerTask == null) {
                start();
            }
        }
    }

    /**
     * 暂停
     */
    public void pause() {
        stop();
        pause = true;
    }

    public void stop() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask = null;
        }
    }

    @Override
    public void showNext() {
        if (createViewListener != null) {
            try {
                createViewListener.setViewData(getNextView(), items.get(index++));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.showNext();
    }

    public void setCreateViewListener(CreateViewListener<T> listener) {
        createViewListener = listener;
    }

    public interface CreateViewListener<T> {
        void setViewData(View view, T data);
    }
}
